package controller.dictionary;

import controller.common.BaseController;
import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.StaticConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.dictionary.LockReasonServices;
import validator.NotBlankFluentValidator;
import wt.entity.dictionary.LockReason;
import wt.entity.dictionary.LockReasonExample;

import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller

@RequestMapping("dictionary/lockReason")
public class LockReasonController extends BaseController {

    @Autowired
    LockReasonServices lockReasonServices;


    @RequiresPermissions("dictionary:lockReason:default")
    @RequestMapping("index")
    public String index() {
        return "dictionary/lockReason/index";
    }

    @RequiresPermissions("dictionary:lockReason:default")
    @RequestMapping("editDlg")
    public String editDlg(@RequestParam(value = "id", required = false) String id, Map map) throws Exception {
        if (id != null) {
            LockReason lockReason;

            lockReason = lockReasonServices.selectByPrimaryKey(id);
            if (lockReason == null) {
                map.put(StaticConstant.ERROR_MESSAGE, StaticConstant.NOT_FOUND_DATA);
                return StaticConstant.ERROR_DLG;
            }
            map.put(StaticConstant.EDIT_DATA, lockReason);
            map.put(StaticConstant.EDIT_URL, "dictionary/lockReason/update");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_UPDATE);
        } else {
            map.put(StaticConstant.EDIT_URL, "dictionary/lockReason/insert");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_INSERT);
        }
        return "dictionary/lockReason/editDlg";
    }

    @RequiresPermissions("dictionary:lockReason:default")
    @ResponseBody
    @RequestMapping("insert")
    public CommonResult insert(LockReason lockReason) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(lockReason.getName(), new NotBlankFluentValidator("名称"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        LockReasonExample lockReasonExample=new LockReasonExample();
        lockReasonExample.createCriteria().andNameEqualTo(lockReason.getName());
        List<LockReason> lockReasonList = lockReasonServices.selectByExample(lockReasonExample);
        if (lockReasonList.isEmpty()){
            lockReasonServices.insertSelective(lockReason);
            return commonResult.setSuccess();
        }else {
            commonResult.setMessage("名称不能重复！");
            return commonResult.setError();
        }


    }


    @RequiresPermissions("dictionary:lockReason:default")
    @ResponseBody
    @RequestMapping("update")
    public CommonResult update(LockReason lockReason) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(lockReason.getName(), new NotBlankFluentValidator("名称"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        LockReasonExample lockReasonExample=new LockReasonExample();
        lockReasonExample.createCriteria().andNameEqualTo(lockReason.getName()).andIdNotEqualTo(lockReason.getId());
        List<LockReason> lockReasonList = lockReasonServices.selectByExample(lockReasonExample);
        if (lockReasonList.isEmpty()){
            lockReasonServices.updateByPrimaryKeySelective(lockReason);
            return commonResult.setSuccess();
        }else {
            commonResult.setMessage("名称不能重复！");
            return commonResult.setError();
        }




    }


    @RequiresPermissions("dictionary:lockReason:default")
    @ResponseBody
    @RequestMapping("delete")
    public CommonResult delete(@RequestParam("id") String id) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        if (StringUtils.isBlank(id)) {
            commonResult.addMessage(StaticConstant.DATA_IS_BLANK);
            return commonResult.setError();
        }
        lockReasonServices.deleteByPrimaryKey(id);
        return commonResult.setSuccess();
    }

    @RequiresPermissions("dictionary:lockReason:default")
    @ResponseBody
    @RequestMapping("select")
    public List<LockReason> select() throws Exception {

        LockReasonExample lockReasonExample=new LockReasonExample();
        lockReasonExample.setOrderByClause("id desc");
        return lockReasonServices.selectByExample(lockReasonExample);
    }

}
